<?php
/**
 * Created by PhpStorm.
 * User: Alan
 * Date: 14-3-19
 * Time: 下午2:19
 */
namespace Addons\Videovote\Controller;

use Home\Controller\AddonsController;

class VideovoteController extends AddonsController
{

    public function doVote()
    {
        if (!is_login()) {
            exit(json_encode(array('status' => 0, 'info' => '请登陆后再投票')));
        }
        $appname = I('POST.appname');
        $table = I('POST.table');
        $row = I('POST.row');
        $aJump = I('POST.jump');

        $message_uid = intval(I('POST.uid'));
        $vote['appname'] = $appname;
        $vote['table'] = $table;
        $vote['row'] = $row;
        $vote['uid'] = is_login();

        if (D('Videovote')->where($vote)->count()) {

            exit(json_encode(array('status' => 0, 'info' => '您已经投票，不能再投票')));
        } else {
            $vote['create_time'] = time();
            if (D('Videovote')->where($vote)->add($vote)) {
				D('VideoContent')->where(array('id'=>$vote['row']))->setInc('vote_count');
                $this->clearCache($vote);
                $user = query_user(array('nickname'),get_uid());
                D('Message')->sendMessage($message_uid,$title = $user['nickname'] . '投票给您', $user['nickname'] . '给您投票',  $aJump , array('id' => $row));
                exit(json_encode(array('status' => 1, 'info' => '感谢您的支持')));
            } else {
                exit(json_encode(array('status' => 0, 'info' => '写入数据库失败')));
            }

        }
    }

    /**
     * @param $vote
     * @auth 陈一枭
     */
    private function clearCache($vote)
    {
        D('Videovote')->clearCache($vote['appname'], $vote['table'], $vote['row']);
    }
}

